package com.sphd.restaurantsystem.fragments.goods;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.GoodInAdapter;
import com.sphd.restaurantsystem.base.LazyLoadFragment;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.GoodInOrOutInfo;
import com.sphd.restaurantsystem.info.InorOutRecordInfo;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.InorOutFragmentPresenterImpl;
import com.sphd.restaurantsystem.view.InorOutFragmentView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/8/19.
 */
public class PutInFragment extends LazyLoadFragment implements InorOutFragmentView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    private RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    private InorOutRecordInfo inorOutRecordInfo;
    private static final String TAG = "PutInFragment";
    InorOutFragmentPresenterImpl inoroutpresenter;
    GoodInAdapter goodInAdapter;
    String day;
    @Override
    public int getLayout() {
        return R.layout.swf_and_recy;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        initEvent();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        inoroutpresenter = new InorOutFragmentPresenterImpl(this);
        initAdapter();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        goodInAdapter=new GoodInAdapter(R.layout.item_in_out_record, new ArrayList<>());
        myRecy.setAdapter(goodInAdapter);
    }

    @Override
    public void loadData() {
        inoroutpresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.
                INOROUTDETAILS + "cgid=" + restaurantInfo.getId() + "&type=0&dishesid=" +
                inorOutRecordInfo.getId() + "&day=" + day);
    }

    public void setInfo(RestaurantInfo restaurantInfo, InorOutRecordInfo inorOutRecordInfo, String day) {
        this.restaurantInfo = restaurantInfo;
        this.inorOutRecordInfo = inorOutRecordInfo;
        this.day=day;
    }

    @Override
    public void recList(List<GoodInOrOutInfo> goodInOrOutInfos) {
        goodInAdapter.setNewData(goodInOrOutInfos);
    }


    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
